window.domain = "aleiye.cn";
var host = location.host;
if(host.indexOf("aleiye.cn") != -1 || host.indexOf("aleiye.cn") != -1){
	host = "http://cloud.aleiye.cn";
}else{
	host = "http://" + host;
}
$(function(){
	
	var param = window.location.search
    if(param){
    	var search = param.substr(1);
    	if (search == "qcloud") {
    		$("#voucher_tr").removeClass("DN");
    	}
    	if(search.indexOf("=") != -1) {
    		var mail = search.split("=")[1];
    		if(mail.indexOf("@") != -1) {
    			$("#a_mail").text(mail)
    			$(".maillink").attr("href", "http://mail." + mail.split("@")[1]);
    		}
    	}
    }
	
	$("#shenqing_btn").click(function(){
		var $form = $("#register_form");
		var email = $("#e_mail", $form);
		var password = $("#password", $form);
		var repassword = $("#re_password", $form);
		var cname = $("#cname", $form);
		var company = $("#company", $form);
		var tel = $("#tel", $form);
		var code = $("#code", $form);
		var voucher = $("#voucher", $form);
		var formValidate = $form.validate({
			rules: {
				e_mail: { // 邮箱
					required: true,
					email : true,
					remote: {
						url: host + "/de/register/checkexist",
						data: {"email": function(){
							return email.val();
						},'p':new Date()},
						async: false
					}
				},
				password: { // 密码
					required: true,
					rangelength: [6, 20]
				},
				re_password: {
					required: true,
					equalTo: password
				},
//				cname: {
//					required: true,
//					rangelength: [2, 20]
//				},
//				company: {
//					required: true
//				},
				tel: {
					required: true,
					number: true,
					maxlength: 14
//				},
//				code: {
//					required: false
				},
				voucher: {
					required: function () {
						return $("#voucher_tr").hasClass("DN") == false;
					},
					maxlength: 18,
					minlength: 18
				}
			},
			errorClass: "error-sp",//指定错误提示css类名
			errorElement: "span",//指定错误元素标签
			onkeyup: false,
			errorPlacement: function(error, element) {//修改错误元素的提示位置  
			    element.parent().after(error);  
			},
			messages: {
				e_mail: {
					required: "请输入邮箱地址",
					email: "邮箱地址格式不正确",
					remote: "该邮箱地址已注册"
				},
				password: {
					required: "请输入登录密码",
					rangelength: "密码长度为{0}到{1}个字符"
				},
				re_password: {
					required: "请输入确认密码",
					equalTo: "两次密码不一致"
				},
//				cname: {
//					required: "请输入姓名",
//					rangelength: "姓名长度为{0}到{1}个字符"
//				},
//				company: {
//					required: "请输入公司名称"
//				},
				tel: {
					required: "请输入电话号码",
					number: "电话号码仅支持数字",
					maxlength: "电话号码长度不能超过11位"
//				},
//				code: {
//					required: "请输入邀请码"
				},
				voucher: {
					required: "请输入腾讯云兑换码",
					maxlength: "腾讯云兑换码长度为18位字符串",
					minlength: "腾讯云兑换码长度为18位字符串"
				}
			}
		});
		if(formValidate.form() || formValidate.form()){
			$(this).addClass("loading-quick");
			$.ajax({
				url: host + "/de/register",
				type: "POST",
				dataType: "json",
				data: {
					userName: email.val(),
					password: password.val(),
					mobileTel: tel.val(),
					email: email.val(),
					voucher: voucher.val(),
//					cname: cname.val(),
					company: company.val()
//					code: code.val()
				},
				success: function(data){
					$("#shenqing_btn").removeClass("loading-quick");
					if(data){
						if(data.status == 1){
							if ($("#code").val()) {
								window.location.href = "register_02.php?mail="+email.val();
							} else if ($("#voucher_tr").hasClass("DN") != null && $("#voucher").val()) {
								alert("注册成功，请登录后台");
								window.location.href = "http://cloud.aleiye.cn/de";
							} else{
								$("form")[0].reset();
								alert("您的注册申请已收到，我们将在两个工作日内联系您开通服务。");
								window.location.href = "index.php";
							}
						}else{
							alert(data.msg);
						}
					}else{
						alert("系统错误请稍后再试");
					}
				},
				error: function(data){
					console.log(data);
					$("#shenqing_btn").removeClass("loading-quick");
					alert("注册申请失败请稍后再试");
				}
			});
		}
	});
});